/**
 * File:   NetDataTableModel.java
 * Author: Gvennet
 */
package scenarios.gui;

import dht.identifier.identifier.Identifiers;
import scenarios.net.NetDataRow;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * todo write javadoc
 */
public final class NetDataTableModel extends AbstractTableModel
{

	private static String[] columnIdentifiers = {
			"Данные",
			"Id данных",
			"Id узла-хранителя"
	};

	private List<NetDataRow> data = new ArrayList<NetDataRow>();

	//@Override

	public int getRowCount()
	{
		return data.size();
	}

	//@Override

	public int getColumnCount()
	{
		return columnIdentifiers.length;
	}

	//@Override

	public String getColumnName(int column)
	{
		return columnIdentifiers[column];
	}

	//@Override

	public Class<?> getColumnClass(int column)
	{
		switch (column)
		{

			case 0:
				return Identifiers.class;
			case 1:
				return String.class;
			case 2:
				return Long.class;
			default:
				throw new AssertionError(column);

		}
	}

	//@Override

	public Object getValueAt(int row, int column)
	{
		switch (column)
		{
			case 0:
				return data.get(row).getValue();
			case 1:
				return data.get(row).getDataId();
			case 2:
				return data.get(row).getHostId();
			default:
				throw new AssertionError(column);
		}

	}

	public void addRow(NetDataRow dataRow)
	{
		if (!data.contains(dataRow))
		{
			data.add(dataRow);
			fireTableRowsInserted(data.size() - 1, data.size() - 1);
		}
	}

	public void removeRow(NetDataRow dataRow)
	{
		NetDataRow tempRow = null;
		for (NetDataRow aData : data)
		{
			if (aData.equals(dataRow))
			{
				tempRow = aData;
				break;
			}
		}
		if (tempRow != null)
		{
			data.remove(tempRow);
			fireTableRowsDeleted(data.size() - 1, data.size() - 1);
		}
	}

	public void newData(List<NetDataRow> list)
	{
		data.clear();
		if (list != null)
		{
			data.addAll(list);
		}
		fireTableDataChanged();
	}
}